
clear
cd $directory


/*****************************************************
Load and Reformat data
*****************************************************/

use "source_data`c(dirsep)'state_election_results.dta"

gen Year2 = substr(Year,1,4) //some extra characters in the 5,6th character locations
destring Year2, gen(year) force
*tab Year2, summ(year)

/*****************************************************
Sample Restriction
*****************************************************/
keep if year>=1960 & year<=2020
egen group = group(state racetype)

/*****************************************************
Make Vars
*****************************************************/



//Find two party vote share
gen Rshare = Rep/(Dem + Rep)

//Flag and drop if independent or third party win
gen absIshare = Ind/(Dem + Rep + Ind + Other)
gen absOshare = Other/(Dem + Rep + Ind + Other)
gen absDshare = Dem/(Dem + Rep + Ind + Other)
gen absRshare = Rep/(Dem + Rep + Ind + Other)

gen IOwinFlag = 0
replace IOwinFlag =1 if absIshare>absDshare  & absIshare>absRshare
replace IOwinFlag =1 if absOshare>absDshare  & absOshare>absRshare

drop if IOwinFlag==1

sort group year

egen RShareSD = sd(Rshare), by(group)
egen RShareMean = mean(Rshare), by(group)


gen obs = 1

collapse (first) RShareSD RShareMean (count) obs, by(state racetype)
ta state if racetype=="governor", summ( obs)
ta state if racetype=="senator", summ( obs)

sort racetype state

//Probability of outcome at each margin
// = density underneath +/- .000005 for race within .001pp; ; +/- .00005 for race within .01pp; +/- .0005 for race within .1pp; 
gen margin_1pp = 2*(.5-normal(-.0005/RShareSD)) //normal is cumulative so .5 minus this function is prob mass from limit to x=.5
gen margin_01pp = 2*(.5-normal(-.00005/RShareSD))
gen margin_001pp = 2*(.5-normal(-.000005/RShareSD))

label var margin_1pp "Pr(Margin<.1pp) (range 0,1)"
label var margin_01pp "Pr(Margin<.01pp) (range 0,1)"
label var margin_001pp "Pr(Margin<.001pp) (range 0,1)"

/*****************************************************
Encode and order states
*****************************************************/

levelsof state, local(statelist)

foreach statename in `statelist'{
	di "`statename'"
}

/*****************************************************
Build Table as Matrix, export
*****************************************************/

*Probability of an outcome within X% is a function of SD only
matrix SENATE = J(52,6,.)
levelsof state, local(statelist)
local row = 2
foreach statename in `statelist'{
	
	qui summ RShareMean 	if racetype=="senator" & state==`"`statename'"'
	matrix SENATE[`row',1]=r(mean)*100
	qui summ RShareSD 		if racetype=="senator" & state==`"`statename'"'
	matrix SENATE[`row',2]=r(mean)*100
	
	qui summ margin_1pp 		if racetype=="senator" & state==`"`statename'"'
	matrix SENATE[`row',4]=r(mean)
	qui summ margin_01pp 		if racetype=="senator" & state==`"`statename'"'
	matrix SENATE[`row',5]=r(mean)
	qui summ margin_001pp 		if racetype=="senator" & state==`"`statename'"'
	matrix SENATE[`row',6]=r(mean)
	//Probability of outcome at each margin
	// = density underneath +/- .000005 for race within .001pp; ; +/- .00005 for race within .01pp; +/- .0005 for race within .1pp; 
	
	local row = `row'+1
}

matrix list SENATE

xml_tab SENATE, save("tables`c(dirsep)'TableA4a") replace


*Probability of an outcome within X% is a function of SD only
matrix GOV = J(52,6,.)
levelsof state, local(statelist)
local row = 2
foreach statename in `statelist'{
	
	qui summ RShareMean 	if racetype=="governor" & state==`"`statename'"'
	matrix GOV[`row',1]=r(mean)*100
	qui summ RShareSD 		if racetype=="governor" & state==`"`statename'"'
	matrix GOV[`row',2]=r(mean)*100
	
	qui summ margin_1pp 		if racetype=="governor" & state==`"`statename'"'
	matrix GOV[`row',4]=r(mean)
	qui summ margin_01pp 		if racetype=="governor" & state==`"`statename'"'
	matrix GOV[`row',5]=r(mean)
	qui summ margin_001pp 		if racetype=="governor" & state==`"`statename'"'
	matrix GOV[`row',6]=r(mean)
	//Probability of outcome at each margin
	// = density underneath +/- .000005 for race within .001pp; ; +/- .00005 for race within .01pp; +/- .0005 for race within .1pp; 
	
	local row = `row'+1
}

matrix list GOV

xml_tab GOV, save("tables`c(dirsep)'TableA4b") replace





/*****************************************************
OECD
*****************************************************/

clear
cd $directory


/*****************************************************
Load and Reformat data
*****************************************************/

use "source_data`c(dirsep)'OECD_election_results.dta"

/*****************************************************
Sample Restriction
*****************************************************/
keep if year>=1988 & year<=2020

//check sample size; check for missing data
*xtset

/*****************************************************
Make Vars
*****************************************************/

egen group = group(polity)

//Find two party vote share
gen Wshare = winner/(winner + loser)

sort group year

egen WShareSD = sd(Wshare), by(group)
egen WShareMean = mean(Wshare), by(group)

gen obs = 1

collapse (first) WShareSD WShareMean (count) obs, by(polity)
ta polity, summ( obs)


//Probability of outcome at each margin
// = density underneath +/- .000005 for race within .001pp; ; +/- .00005 for race within .01pp; +/- .0005 for race within .1pp; 
gen margin_1pp = 2*(.5-normal(-.0005/WShareSD)) //normal is cumulative so .5 minus this function is prob mass from limit to x=.5
gen margin_01pp = 2*(.5-normal(-.00005/WShareSD))
gen margin_001pp = 2*(.5-normal(-.000005/WShareSD))

label var margin_1pp "Pr(Margin<.1pp) (range 0,1)"
label var margin_01pp "Pr(Margin<.01pp) (range 0,1)"
label var margin_001pp "Pr(Margin<.001pp) (range 0,1)"

/*****************************************************
Encode and order states
*****************************************************/

levelsof polity, local(statelist)

foreach statename in `statelist'{
	di "`statename'"
}

/*****************************************************
Build Table as Matrix, export
*****************************************************/

*Probability of an outcome within X% is a function of SD only
matrix C = J(52,6,.)
levelsof polity, local(statelist)
local row = 2
foreach statename in `statelist'{
	
	qui summ WShareMean 	if polity==`"`statename'"'
	matrix C[`row',1]=r(mean)*100
	qui summ WShareSD 		if polity==`"`statename'"'
	matrix C[`row',2]=r(mean)*100
	
	qui summ margin_1pp 		if polity==`"`statename'"'
	matrix C[`row',4]=r(mean)
	qui summ margin_01pp 		if polity==`"`statename'"'
	matrix C[`row',5]=r(mean)
	qui summ margin_001pp 		if polity==`"`statename'"'
	matrix C[`row',6]=r(mean)
	//Probability of outcome at each margin
	// = density underneath +/- .000005 for race within .001pp; ; +/- .00005 for race within .01pp; +/- .0005 for race within .1pp; 
	
	local row = `row'+1
}

matrix list C

xml_tab C, save("tables`c(dirsep)'TableA5") replace



